//
//  UILabel+IHUtil.m
//  platform
//
//  Created by Fleming on 14-5-14.
//  Copyright (c) 2014年 ManYi. All rights reserved.
//

#import "UILabel+Utilities.h"

@implementation UILabel (Utilities)

-(void) setPartString:(NSString *) str  TextAttributes:(NSDictionary *) attrs{
    if (str) {
        
        NSRange range = [self.text rangeOfString:str];
        NSMutableAttributedString *atStr =[[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
        if (atStr == nil) {
            atStr = [[NSMutableAttributedString alloc] initWithString:self.text];
        }
        if (range.location != NSNotFound) {
            [atStr addAttributes:attrs range:range];
            self.attributedText = atStr;
        }
    }
}

-(void) setPreSubPartString:(NSString *) preStr subStr:(NSString *)subStr  TextAttributes:(NSDictionary *) attrs{
    
    NSRange range = [self.text rangeOfString:preStr];
    NSMutableAttributedString *atStr =[[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
    if (atStr == nil) {
        atStr = [[NSMutableAttributedString alloc] initWithString:self.text];
    }
    if (range.location != NSNotFound) {
        [atStr addAttributes:attrs range:range];
    }

    NSInteger index = self.text.length>range.length + range.location?(self.text.length - range.location - range.length):0;
    NSString *tempStr = [self.text substringWithRange:NSMakeRange(range.location + range.length, index)];

    NSRange subRange = [tempStr rangeOfString:subStr];
    [atStr addAttributes:attrs range:NSMakeRange(subRange.location + range.length + range.location, subRange.length)];
    self.attributedText = atStr;
}

-(void) addPrefixString:(NSString *) str  TextAttributes:(NSDictionary *) attrs{

    self.text  = [NSString stringWithFormat:@"%@%@",str,self.text];
    [self setPartString:str TextAttributes:attrs];
}

-(void) addSuffixString:(NSString *) str  TextAttributes:(NSDictionary *) attrs{

    self.text  = [NSString stringWithFormat:@"%@%@",self.text,str];
    [self setPartString:str TextAttributes:attrs];
}


-(void) setPartString:(NSString *) str  TextAttributes:(NSDictionary *) attrs rangeOfStr:(NSString *)rangeOfStr{
    NSRange range = [self.text rangeOfString:rangeOfStr];
    NSMutableAttributedString *atStr =[[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
    if (atStr == nil) {
        atStr = [[NSMutableAttributedString alloc] initWithString:self.text];
    }
    
    [atStr addAttributes:attrs range:range];
    self.attributedText = atStr;
}
@end
